define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        moment = require('moment'),
        tz = require('js/utils/tz');
    require('daterangepicker');

    exports.loadDatePicker = function (options, allowTime) {
        var allowTime = allowTime || false;
        var startDate = tz.fromDate(options.dateRange[0], "YYYY-MM-DD"),
            endDate = tz.fromDate(options.dateRange[1], "YYYY-MM-DD"),
            minDate = '2016-08-13',
            format = 'YYYY-MM-DD',
            ranges = {
                'today': [moment().startOf('day'), moment().endOf('day')],
                'yesterday': [moment().startOf('day').subtract(1, 'days'), moment().endOf('day').subtract(1, 'days')],
                'last 7 days': [moment().startOf('day').subtract(7, 'days'), moment().endOf('day').subtract(1, 'days')],
                'last 30 days': [moment().startOf('day').subtract(30, 'days'), moment().endOf('day').subtract(1, 'days')],
                'last 90 days': [moment().startOf('day').subtract(90, 'days'), moment().endOf('day').subtract(1, 'days')],
            };
        if(allowTime){
            format = 'YYYY-MM-DD HH:mm'
            startDate = tz.fromDate(options.dateRange[0], "YYYY-MM-DD HH:mm");
            endDate = tz.fromDate(options.dateRange[1], "YYYY-MM-DD HH:mm");
            minDate = '2016-08-13 00:00';
            ranges['4 hours'] = [moment().subtract(4, 'hours'), moment().format('YYYY-MM-DD HH:mm')];
            ranges['today'] = [moment().startOf('day'), moment().endOf('day').format('YYYY-MM-DD HH:mm')]
        }
        var pickerOptions = {
            autoApply: true,
            alwaysShowCalendars: true,
            locale: {
                format: format,
                separator: ' ~ '
            },
            startDate: startDate,
            endDate: endDate,
            opens: "center",
            ranges: ranges,
            minDate: minDate,
            maxDate: moment(),
        };
        if(allowTime){
            pickerOptions['timePicker'] = true;
            pickerOptions['timePickerIncrement'] = 1;
        }
        $('#dateFilter_').daterangepicker(pickerOptions, function (start, end, label) {
            if (options.callback) options.callback(start, end, label);
        });
    };
    exports.getDateRange = function () {
        var value = $('#dateFilter_').val(),
            t = value.split('~');
        for (var i = 0; i < t.length; ++i) {
            t[i] = t[i].trim();
        }
        return t;
    };

});